package it.unipd.dei.bd.icineasti.database;

import it.unipd.dei.bd.icineasti.resource.Utente;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Fa un update dei dati di un utente a partire da un'istanza di Utente.
 * 
 * @author Bardelle Renzo
 * @version 1.00
 */

public class UpdateUtenteDatabase {

	private static String stat = "UPDATE utente SET nome=?, cognome=?, codfiscale=?, email=?, password=?, cartacredito=?, newsletter=? WHERE idutente=?";
	private final Connection con;
	
	private final Utente u;
	
	public UpdateUtenteDatabase(Connection con, Utente u) {
		this.con = con;
		this.u = u;
	}

	public void updateUtente() throws SQLException {

		PreparedStatement pstm = null;

		try {
			
			pstm = con.prepareStatement(stat);
			
			pstm.setString(1, u.getNome());
			pstm.setString(2, u.getCognome());
			pstm.setString(3, u.getCodfiscale());
			pstm.setString(4, u.getEmail());
			pstm.setString(5, u.getPassword());
			pstm.setString(6, u.getCartacredito());
			pstm.setBoolean(7, u.getNewsletter());
			pstm.setInt(8, u.getIdutente());
			
			pstm.executeUpdate();
			
		} finally {

			if (pstm != null) {
				pstm.close();
			}

			con.close();
		}
	}
}
